﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frmagenda_pe : Form
    {
        System.Data.SqlClient.SqlConnection con;
        
        public Utilizador util7;
        public Cliente cli33;
        public string dt;
        public string hr;
        public string anie;

        public frmagenda_pe(Utilizador util6, Cliente cli32, string dt, string hr, string anie)
        {
            this.util7 = util6;
            this.cli33 = cli32;
            this.dt = dt;
            this.hr = hr;
            this.anie = anie;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            pesquisa_todos();
        }

        private void pesquisa_todos()
        {
            try
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int cid = cli33.get_id();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_exploracao, tipo_exploracao, nro_animais FROM Exploracao WHERE id_cliente = " + cid + "";
                SqlDataReader readatrib = command1.ExecuteReader();

                this.dataGridView1.Rows.Clear();

                while (readatrib.Read())
                {
                    int i = this.dataGridView1.Rows.Add();

                    this.dataGridView1[0, i].Value = readatrib["id_exploracao"].ToString();
                    this.dataGridView1[1, i].Value = readatrib["tipo_exploracao"].ToString();
                    this.dataGridView1[2, i].Value = readatrib["nro_animais"].ToString();
                }
                con.Close();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdagenda_panome_Click(object sender, EventArgs e)
        {
            if (txtagenda_anome.Text == "")
            {
                MessageBox.Show("Primeiro tem de inserir o nome do cliente a pesquisar", "Insira o nome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int cid = cli33.get_id();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_exploracao, tipo_exploracao, nro_animais FROM Exploracao WHERE tipo_exploracao LIKE '%" + txtagenda_anome.Text + "%' and id_cliente = " + cid +"";
                SqlDataReader readatrib = command1.ExecuteReader();

                this.dataGridView1.Rows.Clear();

                while (readatrib.Read())
                {
                    int i = this.dataGridView1.Rows.Add();

                    this.dataGridView1[0, i].Value = readatrib["id_exploracao"].ToString();
                    this.dataGridView1[1, i].Value = readatrib["tipo_exploracao"].ToString();
                    this.dataGridView1[2, i].Value = readatrib["nro_animais"].ToString();
                }
                con.Close();
            }
        }

        private void cmdagenda_voltar_Click(object sender, EventArgs e)
        {
            frm_agendadet frm1 = new frm_agendadet(util7, cli33, null, null, "voltar", dt, hr, anie);
            this.Hide();
            frm1.ShowDialog();
        }

        private void cmdagenda_adicionar_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                int cid1 = cli33.get_id();
                Exploracao ex1 = new Exploracao(Convert.ToInt32(dataGridView1["id_exploracao", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["tipo_exploracao", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["nro_animais", dataGridView1.CurrentRow.Index].Value.ToString()), Convert.ToInt32(cid1));

                int i = this.dataGridView1.CurrentRow.Index;
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                frm_agendadet frm2 = new frm_agendadet(util7, cli33, null, ex1, "insaniexp", dt, hr, anie);
                this.Hide();
                frm2.ShowDialog();
            }
        }
    }
}
